<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="patTime">
        <el-date-picker
          clearable
          v-model="queryParams.patTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="域名地址" prop="domainUrl">
        <el-input
          v-model="queryParams.domainUrl"
          placeholder="请输入域名地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="personChargeUserId">
        <el-select
          v-model="queryParams.personChargeUserId"
          placeholder="负责人"
          clearable
        >
          <el-option
            v-for="dict in userList"
            :key="dict.id"
            :label="dict.nickName"
            :value="dict.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select
          v-model="queryParams.payStatus"
          placeholder="支付状态"
          clearable
        >
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付类型" prop="payType">
        <el-select
          v-model="queryParams.payType"
          placeholder="支付类型"
          clearable
        >
          <el-option
            v-for="dict in typeList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['payment:payment:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['payment:payment:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['payment:payment:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['payment:payment:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paymentList" @selection-change="handleSelectionChange" border>
      <el-table-column type="index" width="55" align="center" label="序号"/>
      <el-table-column label="订单编号" align="center" prop="orderNumber" min-width="250"/>
      <!--      <el-table-column label="域名名称" align="center" prop="domainUrl" min-width="260"/>-->
      <el-table-column label="负责人" align="center" prop="empNickName" min-width="100"/>
      <el-table-column label="支付类型" align="center" prop="payType" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.payType == 0 ? '支付宝' : '微信' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center" prop="customerName" min-width="100"/>
      <!--      <el-table-column label="手机号" align="center" prop="phone" min-width="180"/>-->
      <el-table-column label="支付金额" align="center" prop="payAmount" min-width="80"/>
      <el-table-column label="支付状态" align="center" prop="payStatus" min-width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.payment_satus" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="patTime" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.patTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" min-width="180"/>

      <!--      <el-table-column label="承接的客服" align="center" prop="userName" min-width="180"/>-->
      <el-table-column label="结果地址" align="center" prop="resulAddress" min-width="280">
        <!--        <template slot-scope="scope">-->
        <!--          <el-input-->
        <!--            v-model="scope.row.resulAddress"-->
        <!--            style="max-width: 600px"-->
        <!--          >-->
        <!--            <template #append>-->
        <!--              <el-button style="padding: unset">🔗</el-button>-->
        <!--            </template>-->
        <!--          </el-input>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="280">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="acceptUpdate(scope.row)"-->
          <!--          >承接-->
          <!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="csResult(scope.row)"
          >查看测算结果
          </el-button>
          <!--          <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['payment:payment:edit']"
                    >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['payment:payment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="支付金额" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入支付金额"/>
        </el-form-item>

        <el-form-item label="支付时间" prop="patTime">
          <el-date-picker clearable
                          v-model="form.patTime"
                          type="date"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结果地址" prop="resulAddress">
          <el-input v-model="form.resulAddress" placeholder="请输入结果地址"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改测算结果对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="1200px" append-to-body>
      <el-form ref="form1" :model="form1" label-width="80px">
        <el-form-item label="订单编号" prop="orderNumber">
          <el-input v-model="form1.orderNumber" placeholder="请订单编号"/>
        </el-form-item>
        <el-form-item label="测算json结果">
          <editor v-model="form1.jsonResult" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancels">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPayment,
  getPayment,
  delPayment,
  addPayment,
  updatePayment,
  updatePayment1,
  updateJsonByOrderNum
} from "@/api/payment/payment";
import {listJson} from "@/api/json/json";
import {listUser} from "@/api/system/user";

export default {
  name: "Payment",
  dicts: ['payment_satus'],
  data() {
    return {
      typeList: [
        {
          value: 1,
          label: '微信'
        },
        {
          value: 0,
          label: '支付宝'
        }
      ],
      statusList: [
        {
          value: 0,
          label: '未支付'
        },
        {
          value: 1,
          label: '已支付'
        }
      ],
      userList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      paymentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open1: false,
      // 弹出层标题
      title1: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        customerName: null,
        phone: null,
        patTime: null,
        domainUrl: null,
        personChargeUserId: null,
        payStatus: null,
        payType: null,
      },
      // 查询参数
      queryParams1: {
        orderNumber: null,
      },
      // 表单参数
      form: {},
      // 测算表单参数
      form1: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getListUser()
  },
  methods: {
    /** 查询用户列表 */
    getListUser() {
      listUser().then(response => {
          this.userList = response.rows;
        }
      );
    },
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listPayment(this.queryParams).then(response => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancels() {
      this.open1 = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        customerId: null,
        customerName: null,
        phone: null,
        payAmount: null,
        payStatus: null,
        patTime: null,
        resulAddress: null,
        userId: null,
        userName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 承接按钮操作 */
    acceptUpdate(row) {
      const id = row.id || this.ids
      getPayment(id).then(response => {
        this.form = response.data;
        if (this.form.userId != null) {
          this.$modal.msgSuccess("已承接，不可重复承接");
        } else {
          updatePayment1(id).then(response => {
            this.$modal.msgSuccess("承接成功");
            this.open = false;
            this.getList();
          });
        }
      });

    },
    /** 测算按钮操作 */
    csResult(row) {
      this.reset();
      console.log("1", row.orderNumber)
      this.queryParams1.orderNumber = row.orderNumber
      listJson(this.queryParams1).then(response => {
        if (response.total == 0) {
          this.$modal.msgSuccess("无测算结果");
        }
        this.form1 = response.rows[0];
        this.open1 = true;
        this.title1 = "测算结果";

      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPayment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          if (this.form1.id != null) {
            updateJsonByOrderNum(this.form1).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + ids + '"的数据项？').then(function () {
        return delPayment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('payment/payment/export', {
        ...this.queryParams
      }, `payment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
